package com.gtq.uhome.service.serviceImpl;

import com.gtq.uhome.mapper.RoleMapper;
import com.gtq.uhome.pojo.Role;
import com.gtq.uhome.service.RoleService;
import com.gtq.uhome.util.TimestampUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

@Service("roleService")
@Transactional(propagation = Propagation. REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;//实例化

    /**
     * 查询全部角色信息
     * @return
     */
    @Override
    public List<Role> selectRoleAll() {
        List<Role> roles = roleMapper.selectByAll();
        return roles;
    }

    /**
     * 根据角色名查询添加时间及角色描述
     * @param roleName
     * @return
     */
    @Override
    public List<Role> selectRole(Role roleName) {
        List<Role> roles = roleMapper.selectBy(roleName);
        return roles;
    }

    /**
     * 添加角色信息
     * @param role
     * @return
     */
    @Override
    @Transactional
    public Boolean insertRole(Role role) {
        List<Role> roles = roleMapper.selectBy(role);
        System.out.println("开始查询"+roles.size());
        int size = roles.size();
        if (size==0){
            System.out.println("开始添加");
            role.setAddTime(TimestampUtil.TimestampTime());
        int insert = roleMapper.insert(role);
        if (insert==1){
            return true;
        }
    }
        return false;
    }

    /**
     * 修改角色表信息，或通过修改code状态实现假删除
     * @param role
     * @return
     */
    @Override
    @Transactional
    public Boolean updateRole(List<Role> role) {
        Boolean delete =false;
        for (Role role1 : role) {
            int i = roleMapper.updateByAll(role1);
            if(i==1){
                delete=true;
            }
        }
        return delete;
    }
}
